unit GroupDataForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Buttons,
  TntForms, TntButtons, TntStdCtrls, ImgList, TntExtCtrls,
  TntClasses;

type

  TLoginState = (lsNo, lsNoChecked, lsOK, lsFail);

  TGroupForm = class(TTntForm)
    PanelMain: TTntPanel;
    LabeledEditDescription: TTntEdit;
    TntLabel3: TTntLabel;
    TntLabel4: TTntLabel;
    LabeledEditName: TTntEdit;
    ImageList: TImageList;
    ButtonCheckLogin: TTntImage;
    TntLabel5: TTntLabel;
    PanelBottom: TTntPanel;
    BitBtnOk: TTntBitBtn;
    BitBtnCancel: TTntBitBtn;
    LabelError: TTntLabel;
    procedure TntFormShow(Sender: TObject);
    procedure LabeledEditLoginChange(Sender: TObject);
    procedure LabeledEditNameChange(Sender: TObject);
    procedure ButtonCheckLoginClick(Sender: TObject);
    procedure BitBtnOkClick(Sender: TObject);
    procedure LabeledEditDescriptionChange(Sender: TObject);
  private
    procedure SetLoginState(aState: TLoginState);
    procedure CheckValues;
    procedure SetPicture(aButton: TTntImage; aPictureNumber: Integer);
    function CheckName(aName: WideString): Boolean;
    function IsDataChanged: Boolean;
  public
    Host, Login, Password: string;
    GroupName, GroupDescription: WideString;
    NewGroupName, NewGroupDescription: WideString;
  end;

implementation

uses
  uInterfaces,
  uRequestImpl,
  uSplashImpl,
  uUtils,
  uTexts;

{$R *.dfm}

{ TStudentForm }

function TGroupForm.CheckName(aName: WideString): Boolean;
var
  request: IgtRequest;
  response: IgtResponse;
  splash: IgtSplash;
begin
  splash := NewSplash(Self);
  splash.ShowSplash(cCheckTitleUnical);
  try
    request := NewRequest(Host, Login, Password);
    request.Param['Group-name'] := Caption;
    response := request.ExecSync('GETGROUP');
    Result := response.Code = 301;
  finally
    splash.HideSplash;
  end;
end;

procedure TGroupForm.CheckValues;
var
  ok: boolean;
  message: WideString;
begin
  ok := True;
  if LabeledEditName.Text = '' then
    begin
    message := cEmptyTitle;
    ok := False;
    end;
  if ok and not IsShortText(LabeledEditName.Text) then
    begin
    message := cLongTitle;
    ok := False;
    end;
  if ok and not IsShortText(LabeledEditDescription.Text) then
    begin
    message := cLongDescription;
    ok := False;
    end;
  BitBtnOk.Enabled := ok;
  if not ok then
    LabelError.Caption := message
  else
    LabelError.Caption := '';
end;

function TGroupForm.IsDataChanged: Boolean;
begin
  if GroupName <> '' then
    Result := (GroupName <> LabeledEditName.Text) or
      (GroupDescription <> LabeledEditDescription.Text)
  else
    Result := ('' <> LabeledEditName.Text) or
      ('' <> LabeledEditDescription.Text);
end;

procedure TGroupForm.LabeledEditDescriptionChange(Sender: TObject);
begin
  CheckValues;
end;

procedure TGroupForm.LabeledEditLoginChange(Sender: TObject);
begin
  if Trim(LabeledEditName.Text) <> '' then
    SetLoginState(lsNoChecked)
  else
    SetLoginState(lsNo);
  CheckValues;
end;

procedure TGroupForm.LabeledEditNameChange(Sender: TObject);
begin
  if Trim(LabeledEditName.Text) <> '' then
    SetLoginState(lsNoChecked)
  else
    SetLoginState(lsNo);
  CheckValues;
end;

procedure TGroupForm.SetLoginState(aState: TLoginState);
begin
  case aState of
    lsNo:
      begin
      SetPicture(ButtonCheckLogin, 0);
      ButtonCheckLogin.Hint := '';
      ButtonCheckLogin.Enabled := False;
      end;
    lsNoChecked:
      begin
      SetPicture(ButtonCheckLogin, 1);
      ButtonCheckLogin.Hint := cCheckTitleAvailable;
      ButtonCheckLogin.Enabled := True;
      end;
    lsOK:
      begin
      SetPicture(ButtonCheckLogin, 2);
      ButtonCheckLogin.Hint := cTitleUnical;
      ButtonCheckLogin.Enabled := False;
      end;
    lsFail:
      begin
      SetPicture(ButtonCheckLogin, 3);
      ButtonCheckLogin.Hint := cGroupAlreadyExists;
      ButtonCheckLogin.Enabled := False;
      end;
  end;
end;

procedure TGroupForm.SetPicture(aButton: TTntImage;
  aPictureNumber: Integer);
var
  btm: TBitmap;
begin
btm := TBitmap.Create;
try
  ImageList.GetBitmap(aPictureNumber, btm);
  btm.TransparentColor := clWhite;
  aButton.Picture.Bitmap := btm;
  aButton.Transparent := True;
finally
  btm.Free;
end;
end;

procedure TGroupForm.TntFormShow(Sender: TObject);
begin
  LabeledEditName.Text := GroupName;
  LabeledEditDescription.Text := GroupDescription;
  if GroupName <> '' then
    SetLoginState(lsOK)
  else
    SetLoginState(lsNo);
  CheckValues;
end;

procedure TGroupForm.BitBtnOkClick(Sender: TObject);
var
  request: IgtRequest;
  response: IgtResponse;
  splash: IgtSplash;
begin
  if not IsDataChanged then
    begin
    ModalResult := mrCancel;
    Exit;
    end;

  splash := NewSplash(Self);
  splash.ShowSplash(cAddGroup);
  try
    request := NewRequest(Host, Login, Password);
    request.Param['Group-name'] := LabeledEditName.Text;
    if GroupName = '' then
      begin
      request.Param['Group-description'] := LabeledEditDescription.Text;
      response := request.ExecSync('ADDGROUP');
      end
    else
      begin
      request.Param['Group-name'] := GroupName;
      request.Param['Group-newname'] := LabeledEditName.Text;
      request.Param['Group-newdescription'] := LabeledEditDescription.Text;
      response := request.ExecSync('EDITGROUP');
      end;
    if response.Code <> 200 then
      begin
      ModalResult := mrCancel;
      ShowWarningMessage(response.MessageText);
      Exit;
      end;
    NewGroupName := LabeledEditName.Text;
    NewGroupDescription := LabeledEditDescription.Text;  
    ModalResult := mrOk;
  finally
    splash.HideSplash;
  end;
end;

procedure TGroupForm.ButtonCheckLoginClick(Sender: TObject);
begin
  if CheckName(LabeledEditName.Text) then
    SetLoginState(lsOK)
  else
    SetLoginState(lsFail);
end;

end.
